package com.xyp.cityipgen.utils;

import com.xyp.cityipgen.model.CityIP;
import com.xyp.cityipgen.model.User;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

/**
 * @author xyp
 * @date 2019-08-07 17:49
 * @description
 * @version: 1.0
 */
public class DateFactory {
    public static List<User> getExcelData(List<CityIP> cityIPList,int num) {
        List<User> list = new ArrayList();
        for(CityIP cityIP : cityIPList){
            try {
                List<String> ipList = getIpCityAddresss(longToIP(Long.parseLong(cityIP.getIPstart())),
                        longToIP(Long.parseLong(cityIP.getIPend())),num);
                for(String newIP : ipList ){
                    User user = new User();

                    user.setAddress(cityIP.getAddress());
                    user.setIp1(newIP);
                    user.setInfo(cityIP.getRemark());
                    //得到往后十天内的随机时间
                    user.setTime(getDate("2019-01-21 00:00:00"));
                    list.add(user);
                }
            }catch (Exception e){
                System.out.println("error>>>>>>>"+e.getMessage());
            }
        }
        return list;
    }

    private static List<String> getIpCityAddresss(String ipStart, String ipEnd,int num){
        List<String> array= new ArrayList<>();
        String  [] ipStartArray = ipStart.split("\\.");
        int first1 = Integer.parseInt(ipStart.split("\\.")[2]);
        int first2 =  Integer.parseInt(ipEnd.split("\\.")[2]);

        int next1 = Integer.parseInt(ipStart.split("\\.")[3]);
        int next2 = Integer.parseInt(ipEnd.split("\\.")[3]);

        if(first1<=first2){
            String newIP="";
            for(int i=0 ;i<(first2-first1);i++){
                newIP=ipStartArray[0]+"."+ipStartArray[1]+"."+(first1+i);
               for(int n=0;n<num;n++){
                   array.add(newIP+"."+new Random().nextInt(255));
               }
            }
        }

        return array;
    }

    private static String getDate(String date) throws ParseException {
        Random random = new Random();
        Random sRandom = new Random();

        int val = random.nextInt(100);
//                2019.1.21-2019.1.31
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parseDate(date,"yyyy-MM-dd HH:mm:ss"));
        calendar.set(Calendar.DAY_OF_MONTH,(random.nextInt(10)+21));
        calendar.set(Calendar.SECOND,sRandom.nextInt(60));
        calendar.set(Calendar.MINUTE,sRandom.nextInt(60));


        if(val<80){
            Random hourRandom = new Random();
            int hour = hourRandom.nextInt(12);

            calendar.set(Calendar.HOUR,(hour+8));
        }else if(val<95){
            Random hourRandom = new Random();
            int hour = hourRandom.nextInt(8);

            calendar.set(Calendar.HOUR,(hour));
        }else {
            Random hourRandom = new Random();
            int hour = hourRandom.nextInt(3);

            calendar.set(Calendar.HOUR,(hour+20));
        }

        return DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
    }


    public static String longToIP(long longIp) {
        StringBuffer sb = new StringBuffer("");
        // 直接右移24位
        sb.append(String.valueOf((longIp >>> 24)));
        sb.append(".");
        // 将高8位置0，然后右移16位
        sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
        sb.append(".");
        // 将高16位置0，然后右移8位
        sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
        sb.append(".");
        // 将高24位置0
        sb.append(String.valueOf((longIp & 0x000000FF)));
        return sb.toString();
    }
}
